<template>
  <el-card class="box_card">
    <template #header>
      <div class="manage_list_query">
        <el-form
          :inline="true"
          label-position="left"
          :model="manageListQuery"
          class="demo-form-inline"
        >
          <el-form-item label="管理员名称：">
            <el-input
              v-model="manageListQuery.manageName"
              clearable
              placeholder="管理员名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="管理员账号：">
            <el-input
              v-model="manageListQuery.managePhone"
              clearable
              placeholder="管理员账号"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitQuery">查询</el-button>
            <el-button type="primary" @click="addManage">
              <span class="iconify" data-icon="subway:add-1" data-width="10">
              </span>
              添加管理员
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>
    <el-table
      :data="managerList"
      style="width: 100%"
      row-key="id"
      :header-cell-style="{
        'text-align': 'center',
      }"
      :cell-style="{
        'text-align': 'center',
      }"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="manageName" label="管理员名称" width="220" />
      <el-table-column prop="managePhone" label="管理员账号" />

      <el-table-column label="可用状态">
        <template v-slot:default="scope">
          <el-switch
            v-model="scope.row.manageStatus"
            @change="changeManageStatus(scope.row.id, scope.row.manageStatus)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="160" fixed="right">
        <template v-slot:default="scope">
          <el-button
            size="small"
            type="primary"
            @click="modifyManage(scope.row.id)"
          >
            编辑
          </el-button>
          <el-button
            size="small"
            type="danger"
            @click="deleteManage(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import { onMounted, ref, reactive } from 'vue'
import {
  getManageListApi,
  deleteManageApi,
  updateManageStatusApi,
} from '@/api/manage'
import { useRouter } from 'vue-router'

const router = useRouter()
onMounted(() => {
  getManageList()
})

const manageListQuery = reactive({
  manageName: '',
  managePhone: '',
})
// ? 点击根据条件查询管理员列表
const submitQuery = () => {
  getManageList()
}
// ? 跳转到添加管理员页面
const addManage = () => {
  router.push('/manage/add')
}

// ? 跳转到修改管理员页面
const modifyManage = (id: string) => {
  router.push({
    path: '/manage/modify',
    query: {
      id,
    },
  })
}

// ? 删除管理员的方法
const deleteManage = async (id: string) => {
  let res: any = await deleteManageApi(id)
  console.log(res)
  if (res.status === 0) {
    ElMessage.success('删除管理员成功')
    getManageList()
  }
}
const changeManageStatus = async (id: string, status: boolean) => {
  let res: any = await updateManageStatusApi(id, status)
  if (res.status == 0) {
    ElMessage.success(res.msg)
    getManageList()
  }
}
// * 管理员列表
const managerList = ref([])
/**
 * 获取管理员列表的方法
 */
const getManageList = async () => {
  var res: any = await getManageListApi(manageListQuery)
  if (res.status === 0) {
    managerList.value = res.data
  }
}
</script>
<style lang="scss" scoped>
:v-deep(.el-card__header) {
  padding-bottom: 0;
}

.box_card {
  margin: $mainMargin;
}
</style>
